package com.spring.demo10.eg2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author vhans
 */
@ComponentScan
public class Test {

    /**
     * 当自定义的Scope中proxyMode=ScopedProxyMode.TARGET_CLASS的
     * 时候，会给这个bean创建一个代理对象，调用代理对象的任何方法，都会调用这个自定义的作用域实现
     * 类（本案例中是BeanMyScope）中get方法来重新来获取这个bean对象。
     */
    @org.junit.Test
    public void test() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 将自定义作用域注册到spring容器中
        context.getBeanFactory().registerScope(BeanMyScope.SCOPE_MY, new BeanMyScope());
        context.register(Test.class);
        context.refresh();

        System.out.println("从容器中获取User对象");
        User user = context.getBean(User.class);
        System.out.println("User对象为" + user.getClass());

        System.out.println("多次调用user的getUserName\n");
        for (int i = 1; i <= 3; i++) {
            System.out.printf("=========第%d次调用开始\n", i);
            System.out.println("username=" + user.getUsername());
            System.out.printf("=========第%d次调用结束\n", i);
        }
    }
}
